package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.ParmasException;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findPage(Integer page, Integer pageSize, String name) {
        //创建条件查询
        QueryWrapper<Setmeal> wrapper =new QueryWrapper<>();
        //进行模糊查询
        wrapper.like(StringUtils.isNotBlank(name),"name",name);
        wrapper.orderByDesc("update_time");
        //封装条件查询和分页的信息
        Page<Setmeal> pageInfo1 = this.page(new Page<Setmeal>(page, pageSize), wrapper);
        //把pageInfo1<Setmeal>的信息拷贝到pageInfo2<SetmealDto>
        Page<SetmealDto> pageInfo2 = new Page<>(page, pageSize);
        //拷贝除records以外的所有信息
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        //拷贝records集合
        ArrayList<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : pageInfo1.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            //设置分类名称
            Category category = categoryService.getById(setmealDto.getCategoryId());
            //获取套餐表里面的名字
            setmealDto.setCategoryName(category.getName());

            setmealDtoList.add(setmealDto);
        }
        pageInfo2.setRecords(setmealDtoList);

        return pageInfo2;
    }

    /**
     * 添加套餐
     * @param setmealDto
     */

    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        //保存套餐表数据信息
        this.save(setmealDto);
        //保存套餐菜品关系信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //获取setSetmealId这个的id值
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 数据回显
     * @param id
     * @return
     */

    @Override
    public SetmealDto findWithSetmealDish(Long id) {
        //先查询套餐数据信息
        Setmeal setmeal = this.getById(id);

        //创建SetmealDto对象 把setmeal拷贝到SetmealDto
        SetmealDto setmealDto = new SetmealDto();
        //在查询菜品数据信息
        QueryWrapper<SetmealDish> wrapper =new QueryWrapper<>();

        wrapper.eq("setmeal_id",setmeal.getId());

        List<SetmealDish> list = setmealDishService.list(wrapper);

        setmealDto.setSetmealDishes(list);
        BeanUtils.copyProperties(setmeal,setmealDto);

        return setmealDto;
    }

    /**
     * 批量删除
     * @param ids
     */

    @Override
    public void deleteByIds(List<Long> ids) {
        //判断套餐状态status==1
        QueryWrapper<Setmeal> wrapper1 = new QueryWrapper<>();
        wrapper1.in("id",ids);
        wrapper1.eq("status",1);
        int count = this.count(wrapper1);
        if (count>0){
            throw new ParmasException("当前菜品正在出售,无法删除");
        }
        //1.删除套餐信息
       this.removeByIds(ids);
        //2.删除套餐里面的菜品数据 delete from setmeal_dish where setmeal_id in ids
        QueryWrapper<SetmealDish> wrapper =new QueryWrapper<>();
        wrapper.in("setmeal_id",ids);
        setmealDishService.list(wrapper);

    }

    /**
     * 批量启停
     * @param status
     * @param ids
     */
    @Override
    public void statusUpdate(Integer status, Long[] ids) {
        //遍历ids数组获取setmeal对象
        for (Long id : ids) {
            Setmeal setmeal = this.getById(id);
            //改变status的状态
            setmeal.setStatus(status);
            this.updateById(setmeal);
        }
    }


}
